var express = require('express')
var websocketApi = express.Router()// 创建一个路由实例

var expressWs = require('express-ws');
expressWs(websocketApi);


websocketApi.use(function (req, res, next) {
    console.log('middleware');
    req.testing = 'testing';
    return next();
});

websocketApi.get('/', function (req, res, next) {
    console.log('get route', req.testing);
    res.end();
});

websocketApi.ws('/socketTest', function (ws, req) {
    console.log('websocket 连接成功 :----> ')
    ws.on('message', function (msg) {
        console.log('websocket 收到信息 :----> ' + msg)
        ws.send("server back msg ")
    });
    console.log('socket', req.testing);
    setTimeout(() => {
        ws.send('server close for reclonnect')
        ws.close()
    }, 10000);
});

// app.listen(3000)
module.exports = websocketApi